package com.example.photography.Activity;

import android.content.Context;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.example.photography.R;
import com.example.photography.adapter.store.HomeAdapter;
import com.example.photography.adapter.store.MenuAdapter;
import com.example.photography.bean.CategoryBean;
import com.example.photography.common.BaseActivity;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class Store_Activity extends BaseActivity {


    @BindView(R.id.lv_menu)
    ListView lvMenu;
    @BindView(R.id.lv_home)
    ListView lvHome;
    @BindView(R.id.tv_titile)
    TextView tvTitile;
    private MenuAdapter menuAdapter;
    private HomeAdapter homeAdapter;
    private List<String> menuList = new ArrayList<>();
    private List<CategoryBean.DataBean> homeList = new ArrayList<>();
    private List<Integer> showTitle;
    private int currentItem;

    @Override
    protected void initData() {
        initView();
        loadData();
    }


    private void loadData() {
        String json = getJson(this, "category.json");
        CategoryBean categoryBean = JSONObject.parseObject(json, CategoryBean.class);
        showTitle = new ArrayList<>();
        for (int i = 0; i < categoryBean.getData().size(); i++) {
            CategoryBean.DataBean dataBean = categoryBean.getData().get(i);
            menuList.add(dataBean.getModuleTitle());
            showTitle.add(i);
            homeList.add(dataBean);
        }
        tvTitile.setText(categoryBean.getData().get(0).getModuleTitle());

        menuAdapter.notifyDataSetChanged();
        homeAdapter.notifyDataSetChanged();
    }

    private void initView() {
        menuAdapter = new MenuAdapter(menuList, this);
        lvMenu.setAdapter(menuAdapter);

        homeAdapter = new HomeAdapter(homeList, this);
        lvHome.setAdapter(homeAdapter);

        lvMenu.setOnItemClickListener(new AdapterView.OnItemClickListener() {//点击事件
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                menuAdapter.setSelectItem(position);
                menuAdapter.notifyDataSetInvalidated();
                tvTitile.setText(menuList.get(position));
                lvHome.setSelection(showTitle.get(position));///让ListView定位到指定Item的位置

            }
        });


        lvHome.setOnScrollListener(new AbsListView.OnScrollListener() {//触屏滑动
            private int scrollState;

            /**
             *
             * @param absListView
             * @param scrollState
             */
            @Override
            public void onScrollStateChanged(AbsListView absListView, int scrollState) {
                this.scrollState = scrollState;
            }

            /**
             *
             * @param absListView
             * @param firstVisibleItem 第一个可见项是ListView的第几项(下标从0开始)
             * @param visibleItemCount 可见项的总数
             * @param totalItemCount 总项数
             */
            @Override
            public void onScroll(AbsListView absListView, int firstVisibleItem,
                                 int visibleItemCount, int totalItemCount) {
                // 当滑动停止的时候是否已经到达了底部
                if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {
                    return;
                }
                //集合中某个元素出现的位置
                int current = showTitle.indexOf(firstVisibleItem);
                if (currentItem != current && current >= 0) {//判断目前home的title是否需要更新
                    currentItem = current;
                    tvTitile.setText(menuList.get(currentItem));//更新hometitle
                    menuAdapter.setSelectItem(currentItem);
                    menuAdapter.notifyDataSetInvalidated();
                }
            }
        });
    }

    /**
     * 得到json文件中的内容
     *
     * @param context
     * @param fileName
     * @return
     */
    public static String getJson(Context context, String fileName) {
        StringBuilder stringBuilder = new StringBuilder();
        //获得assets资源管理器
        AssetManager assetManager = context.getAssets();
        //使用IO流读取json文件内容
        try {
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(
                    assetManager.open(fileName), "utf-8"));
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                stringBuilder.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return stringBuilder.toString();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_store;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
